package bg.cytec.android.fskmodem;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import bg.cytec.android.fskmodem.FSKDecoder.FSKDecoderCallback;
import bg.cytec.android.wav.WavToPCM;
import bg.cytec.android.wav.WavToPCM.WavInfo;

public class DecodeWavActivity extends Activity {
	protected FSKConfig mConfig;
	protected FSKDecoder mDecoder;
	
	protected FSKEncoder mEnCoder;
	
	protected Runnable mPCMFeeder = new Runnable() {
		
		@Override
		public void run() {
			try {
				//open input stream to the WAV file
				InputStream input = getResources().getAssets().open("topway_left.wav");

				//get information about the WAV file
				WavInfo info = WavToPCM.readHeader(input);
				
				//get the raw PCM data
				ByteBuffer pcm = ByteBuffer.wrap(WavToPCM.readWavPcm(info, input));

				//the decoder has 1 second buffer (equals to sample rate), 
				//so we have to fragment the entire file, 
				//to prevent buffer overflow or rejection
				byte[] buffer = new byte[1024];
						
				//feed signal little by little... another way to do that is to 
				//check the returning value of appendSignal(), it returns the 
				//remaining space in the decoder signal buffer
				while (pcm.hasRemaining()) {
					
					if (pcm.remaining() > 1024) {
						pcm.get(buffer);
					}
					else {
						buffer = new byte[pcm.remaining()];
						
						pcm.get(buffer);
					}
					
					mDecoder.appendSignal(buffer);
					
					Thread.sleep(100);
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_wav);
		
		/// INIT FSK CONFIG
		
		try {
			mConfig = new FSKConfig(FSKConfig.SAMPLE_RATE_29400, FSKConfig.PCM_8BIT, FSKConfig.CHANNELS_MONO, FSKConfig.SOFT_MODEM_MODE_4, FSKConfig.THRESHOLD_20P);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		/// INIT FSK DECODER
		
		mDecoder = new FSKDecoder(mConfig, new FSKDecoderCallback() {
			
			@Override
			public void decoded(byte[] newData) {
				
				final String text = new String(newData);
				
				runOnUiThread(new Runnable() {
					public void run() {
						
						TextView view = ((TextView) findViewById(R.id.result));
						
						view.setText(view.getText()+text);
					}
				});
			}
		});
		
		///
		/*mEnCoder = new FSKEncoder(mConfig, new FSKEncoder.FSKEncoderCallback() {
			@Override
			public void encoded(byte[] pcm8, short[] pcm16) {
				
			}
		});*/
		
		new Thread(mPCMFeeder).start();
	}

	@Override
	protected void onDestroy() {
		
		mDecoder.stop();
		
		super.onDestroy();
	}
	
}
